package com.bdqn.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bdqn.entity.academic.School;
import com.bdqn.error.CommonException;
import com.bdqn.response.ResultCode;


import java.util.List;


public class JSONUtils {

    public static<T> List<T> ObjectToList(Object o, Class<T> clazz) throws CommonException {
        if(o == null) throw new CommonException(ResultCode.FAIL);
        try{
            String string = JSON.toJSONString(o);
            List<T> list = JSONArray.parseArray(string, clazz);
            return list;
        }catch (Exception e){
            throw new ClassCastException(e.getMessage());
        }
    }


    public static<T> T ObjectToJavaBean(Object o,Class<T> clazz) throws CommonException {
        if(o == null) throw new CommonException(ResultCode.FAIL);
        T t = JSON.parseObject(JSON.toJSONString(o), clazz);
        return t;
    }


}